package com.xiaoma.tpo.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaoma.tpo.dao.BonusSignRuleDao;
import com.xiaoma.tpo.domain.BonusSignRule;

@Service
public class BonusRuleService {

    @Autowired
    private BonusSignRuleDao signRuleDao;
    
    public Map<String, Object> getSignPoint(int signCount) {
        List<BonusSignRule> rules = signRuleDao.findAll();
        Map<String, Object> result = new HashMap<String, Object>();
        for (int i = 0; i < rules.size(); i++) {
            if (signCount < rules.get(i).getSignCount()) {
                result.put("point", rules.get(i).getBonusPoint());
                result.put("bonusSignRule", rules.get(i-1));
                break;
            } else if (signCount == rules.get(i).getSignCount()) {
                result.put("point", rules.get(i).getBonusPoint() + rules.get(i).getExtraPoint());
                result.put("bonusSignRule", rules.get(i));
                break;
            } else {
                if(i == rules.size() - 1) {
                    result.put("point", rules.get(i).getBonusPoint());
                    result.put("bonusSignRule", rules.get(i));
                }
            }
        }
        result.put("rules", rules);
        return result;
    }
}
